
// ratio value to correct impules
static double ratio = 0.9;

//outpin
int pin = 12;

volatile unsigned int count = 0;

volatile long smooth_count = 0;
volatile long array_count[2] = {0,0};
volatile long time = 0;
volatile long prevtime = 0;

void setup()
{
  pinMode(pin, OUTPUT);
  noInterrupts();      
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1 = 34286;
  TCCR1B |= (1 << CS12);
  TIMSK1 |= (1 << TOIE1);
  interrupts();
  attachInterrupt(0, getPulse, CHANGE);
}

void getPulse() {
 count++;
 time = (micros() - prevtime);
 prevtime = micros();
}

ISR(TIMER1_OVF_vect)        
{
  TCNT1 = 34286;
   if (count == 0 || time > 2000000) { time = 0; }
  array_count[0] = array_count[1];
  array_count[1] = time;
  smooth_count = (array_count[0] + array_count[1])/2 * ratio;
  count = 0;
}

void loop()
{
 if(smooth_count < 2000000 && smooth_count > 10000) {
  digitalWrite(pin, HIGH);
  delay(smooth_count/1000);
  digitalWrite(pin, LOW);
  delay(smooth_count/1000);
 } else {
  digitalWrite(pin, LOW);
 }
}

